<template>
	<view class="view">
		<view class="head">
			<swiper class="banner" @change="e => (current = e.detail.current)">
				<swiper-item v-for="(item, index) in imageList" :key="index"><image :src="item" mode="widthFix"></image></swiper-item>
			</swiper>
			<view class="dots">{{ current + 1 }}/{{ imageList.length }}</view>
		</view>
		<view class="info">
			<view class="name">{{ data.productName }}</view>
			<view class="price-sale">
				<view class="price">
					{{ data.price }}
					<text>积分</text>
				</view>
				<view class="sale">销量：{{ data.salesNum || 0 }}</view>
			</view>
		</view>
		<view class="detail">
			<view class="title">商品详情</view>
			<view class="content" v-html="data.detail"></view>
		</view>
		<view class="bottom"><view class="btn" @click.stop="recharge()">立即兑换</view></view>
	</view>
</template>

<script>
import { formatRichText } from 'utils/index.js';

export default {
	data() {
		return {
			id: null,
			data: {},
			current: 0,
			imageList: []
		};
	},
	onLoad(options) {
		this.id = options.id;
		this.init();
	},
	methods: {
		init() {
			this.$api.http(
				'/product/pmsIntegralProduct/api/n/details',
				'GET',
				{
					productId: this.id
				},
				res => {
					if (res.data.code == 200) {
						res.data.result.detail = formatRichText(res.data.result.detail);
						this.data = res.data.result;
						this.imageList = this.data.bannerPic ? this.data.bannerPic.split(',') : this.data.productPic;
					} else {
						this.$Toast(res.data.message || '请求失败，请重试！');
					}
				}
			);
		},
		recharge() {
			this.$api.http(
				'/product/pmsIntegralProduct/confirmRedemptionEligibility',
				'GET',
				{
					productId: this.id,
					productNum: 1,
					userId: uni.getStorageSync('userInfo').id
				},
				res => {
					if (res.data.code == 200) {
						// 前往兑换
						uni.navigateTo({
							url: '/pages/other/integral/recharge?id=' + this.id
						});
					} else if (res.data.code == 4002) {
						this.showReward = false;
						this.rewardValue = null;
						uni.showModal({
							title: '积分不足',
							content: '是否前往充值积分？',
							confirmText: '充值',
							success(res) {
								if (res.confirm) {
									// 前往充值界面
									uni.navigateTo({
										url: '/pages/other/deposit/deposit'
									});
								}
							}
						});
					} else {
						this.$Toast(res.data.message || '兑换失败，请重试！');
					}
				}
			);
		}
	}
};
</script>

<style lang="less">
.view {
	width: 100%;
	background-color: #f1f1f1;

	.head {
		position: relative;
		width: 100%;
		background-color: #ffffff;

		.banner {
			height: 750rpx;

			image {
				width: 100%;
			}
		}

		.dots {
			position: absolute;
			right: 20rpx;
			bottom: 20rpx;
			width: 80rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border-radius: 50%;
			font-size: 32rpx;
			color: #ffffff;
			background-color: rgba(0, 0, 0, 0.8);
		}
	}

	.info {
		padding: 20rpx;
		background-color: #ffffff;

		.name {
			margin-bottom: 30rpx;
			color: #333333;
			font-size: 30rpx;
		}

		.price-sale {
			display: flex;
			flex-direction: row;
			align-items: center;
			justify-content: space-between;
			font-size: 24rpx;
			color: #999999;

			.price {
				font-size: 38rpx;
				color: #e5ae54;

				text {
					font-size: 20rpx;
				}
			}
		}
	}

	.detail {
		margin-top: 20rpx;
		margin-bottom: 120rpx;
		padding: 20rpx;
		background-color: #ffffff;

		.title {
			padding: 20rpx 0rpx 30rpx 0rpx;
			font-size: 30rpx;
			color: #666666;
		}

		.content {
			width: 100%;
			overflow: hidden;
		}
	}

	.bottom {
		position: fixed;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: #ffffff;

		.btn {
			margin: 20rpx;
			height: 80rpx;
			line-height: 80rpx;
			text-align: center;
			border-radius: 40rpx;
			font-size: 30rpx;
			color: #522c00;
			background-color: #e5ae54;
		}
	}
}
</style>
